﻿using PJTY.dal;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection.Emit;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class views_customer_yhfk : System.Web.UI.Page
{
    DataClasses1DataContext db = new DataClasses1DataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox CheckBox2 = (CheckBox)sender;
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox CheckBox1 = (CheckBox)row.FindControl("CheckBox1");
            CheckBox1.Checked = CheckBox2.Checked;
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        string productName = row.Cells[5].Text;

        var productStats = GetProductStatistics(productName);

        Label3.Text = "你选择的商品是：" + productName;
        Label1.Text = "用户平均年龄为：" + productStats.AverageAge.ToString("0.00");
        Label2.Text = "商品平均分数为：" + productStats.AverageScore.ToString("0.00") + "(最高分为5分)";
        Label4.Text = "商品好评数量为：" + productStats.GoodCount.ToString();
        Label5.Text = "商品中评数量为：" + productStats.MediumCount.ToString();
        Label6.Text = "商品差评数量为：" + productStats.BadCount.ToString();
    }

    private ProductStatistics GetProductStatistics(string productName)
    {
        try
        {
            var reviews = (from dr in db.Review
                           where dr.ProductName == productName
                           select new
                           {
                               Score = dr.ReviewScore,
                               Age = dr.ConsumerAge
                           }).ToArray();

            int goodCount = reviews.Count(r => r.Score > 3);
            int mediumCount = reviews.Count(r => r.Score == 3);
            int badCount = reviews.Count(r => r.Score < 3);
            double averageScore = reviews.Average(r => r.Score);
            double averageAge = reviews.Average(r => (double)r.Age);

            return new ProductStatistics
            {
                GoodCount = goodCount,
                MediumCount = mediumCount,
                BadCount = badCount,
                AverageScore = averageScore,
                AverageAge = averageAge
            };
        }
        catch (Exception ex)
        {
            // Log the exception and handle it appropriately
            throw new ApplicationException("Error retrieving product statistics.", ex);
        }
    }

    private class ProductStatistics
    {
        public int GoodCount { get; set; }
        public int MediumCount { get; set; }
        public int BadCount { get; set; }
        public double AverageScore { get; set; }
        public double AverageAge { get; set; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Excel.Application xlApp = new Excel.Application();
        //Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
        //Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets1;
        //xlWorkSheet.Name = "商品评价数据"; 
        //xlWorkSheet.Cells[1, 1] = "商品名称"; 
        //xlWorkSheet.Cells[1, 2] = "用户平均年龄"; 
        //xlWorkSheet.Cells[1, 3] = "商品平均分数";
        //xlWorkSheet.Cells[1, 4] = "商品好评数量"; 
        //xlWorkSheet.Cells[1, 5] = "商品中评数量";
        //xlWorkSheet.Cells[1, 6] = "商品差评数量";
        //for (int i = 0; i < GridView1.Rows.Count; i++)
        //{
        //    string productName = GridView1.Rows[i].Cells[5].Text;
        //    int[] scores = (from dr in db.Review where dr.ProductName.ToString() == productName select Convert.ToInt32(dr.ReviewScore)).ToArray();
        //    int[] ages = (from dr in db.Review where dr.ProductName.ToString() == productName select Convert.ToInt32(dr.ConsumerAge)).ToArray(); 
        //    int goodCount = (from dr in db.Review where dr.ProductName.ToString() == productName && Convert.ToInt32(dr.ReviewScore) > 3 select dr).Count(); 
        //    int mediumCount = (from dr in db.Review where dr.ProductName.ToString() == productName && Convert.ToInt32(dr.ReviewScore) == 3 select dr).Count();
        //    int badCount = (from dr in db.Review where dr.ProductName.ToString() == productName && Convert.ToInt32(dr.ReviewScore) < 3 select dr).Count(); 
        //    double avgScore = scores.Average(); double avgAge = ages.Average();
        //    xlWorkSheet.Cells[i + 2, 1] = productName; xlWorkSheet.Cells[i + 2, 2] = avgAge.ToString("0.00");
        //    xlWorkSheet.Cells[i + 2, 3] = avgScore.ToString("0.00"); xlWorkSheet.Cells[i + 2, 4] = goodCount.ToString();
        //    xlWorkSheet.Cells[i + 2, 5] = mediumCount.ToString(); xlWorkSheet.Cells[i + 2, 6] = badCount.ToString();
        //    xlWorkSheet.Columns.AutoFit();
        //    xlWorkBook.SaveAs(Server.MapPath("~/商品评价数据.xlsx"));
        //    xlWorkBook.Close(); 
        //    xlApp.Quit(); 
        //    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); 
        //    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook); 
        //    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet); 
        //    Response.Write("<script>alert(‘导出数据成功！’)</script>"); 
        //}
    }
        
}